package com.wenwu.controller.movable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.wenwu.common.bean.RelicsMWenwu;
import com.wenwu.common.page.PageUtils;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.common.utils.JsonUtils;
import com.wenwu.service.MoveableRegisterService;

/**
 * 可移动文物登记表
 * 
 * 基本信息功能
 * 
 * @author Yaphet
 *
 */
@Controller(value = "MoveableRegisterController")
@RequestMapping(value = "/mgr/mregister")
public class MoveableRegisterController {

	Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private MoveableRegisterService moveableRegisterService;
	
	private JsonGenerator jsonGenerator;
	

	
	/**
	 * 初始化界面
	 * @return
	 */
	@RequestMapping(value = "/init")
	public String initUserPage() {
		log.info("进入基本信息管理页面...");
		return "mregister/init";
	}
	
	/**
	 * 进入新增表单页面
	 * @return
	 */
	@RequestMapping(value = "/registerform")
	public String dictionaryform() {
		log.info("进入基本信息表单页面...");
		return "mregister/registerform";
	}
	
	/**
	 * 添加可移动文物登记表信息
	 */
	@RequestMapping(value = "/addregister")
	public void addBasic(RelicsMWenwu relicsMWenwu,HttpServletResponse response) throws Exception {
		jsonGenerator = JsonUtils.getJsonGenerator(response);
		log.info("添加/修改数据："+relicsMWenwu.getmRegisterId());
		try {
			jsonGenerator.writeObject(moveableRegisterService.addObj(relicsMWenwu));
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage(),e);
			jsonGenerator.writeObject(e.getMessage());
		}
	}
	
	/**
	 * 可移动文物登记表表单查询
	 * @return
	 */
	@RequestMapping(value = "/showList")
	public void showList(RelicsMWenwu relicsMWenwu,Model model, HttpServletRequest request,
			HttpServletResponse response) {
		log.info("查询可移动文物登记表表单..." );
		PaginationVO pageVo = PageUtils.getPaginationInfo(request);
		try {
			if(relicsMWenwu!=null && relicsMWenwu.getmRelicsNo() != null){
				relicsMWenwu.setmRelicsNo(relicsMWenwu.getmRelicsNo().trim());
			}
			if(relicsMWenwu!=null && relicsMWenwu.getmName() != null){
				relicsMWenwu.setmName(relicsMWenwu.getmName().trim());
			}
			if(relicsMWenwu!=null && relicsMWenwu.getHistoryYear() != null){
				relicsMWenwu.setmType(relicsMWenwu.getmType().trim());
			}
			if(relicsMWenwu!=null && relicsMWenwu.getHistoryYear() != null){
				relicsMWenwu.setHistoryYear(relicsMWenwu.getHistoryYear().trim());
			}
			
			
			pageVo = moveableRegisterService.selectByCondition(relicsMWenwu, pageVo);
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			jsonGenerator.writeObject(pageVo);// 将PaginationVO对象以json格式写回页面
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage(),e);
			model.addAttribute("errorMsg", "系统异常，请稍后重试!");
		}
	}
	
	/**
	 * 删除信息
	 * 
	 * @param response
	 * @param request
	 * @param key
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteInfo")
	public void deleteInfo(HttpServletResponse response,
			HttpServletRequest request,
			@RequestParam(required = false, value = "key") String key)
			throws Exception {
		jsonGenerator = JsonUtils.getJsonGenerator(response);
		try {
			String result = moveableRegisterService.deleteBykeys(key);
			jsonGenerator.writeObject(result);
		} catch (Exception e) {
			log.info(e.getMessage(),e);
			jsonGenerator.writeObject("删除数据失败");
		}
	}
	
	/**
	 * 显示要修改的信息
	 * 
	 * @param response
	 * @param userId
	 * @throws Exception
	 */
	@RequestMapping(value = "/showUpdate")
	public void showUpdate(HttpServletResponse response,
			@RequestParam(value = "mRelicsId") Integer mRelicsId){
		log.info("mRelicsId:"+mRelicsId);
		if (mRelicsId != null) {
			try{
				RelicsMWenwu relicsMWenwu = moveableRegisterService.selectObjByKey(mRelicsId);
				jsonGenerator = JsonUtils.getJsonGenerator(response);
				jsonGenerator.writeObject(relicsMWenwu);
			}catch (Exception e){
				log.info(e.getMessage(),e);
			}
		}
	}

}
